<?php

/**
 * This is the model class for table "ld_lode".
 *
 * The followings are the available columns in table 'ld_lode':
 * @property integer $serialno
 * @property string $choices
 * @property float $pet
 * @property integer $lode_type
 * @property integer $player_id
 * @property string $play_date
 */
class LoDe extends CActiveRecord
{

    /**
     * Returns the static model of the specified AR class.
     * @return BaseLoDe the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'ld_lode';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('lode_type, player_id', 'numerical', 'integerOnly' => true),
            array('choices', 'length', 'max' => 300),
            array('play_date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('serialno, choices, lode_type, player_id, play_date', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'serialno' => 'Serialno',
            'choices' => 'Choices',
            'lode_type' => 'Lode Type',
            'player_id' => 'Player',
            'play_date' => 'Play Date',
        );
    }

    public static function layDanhSachLuotChoiHomNay()
    {
        $start_time = date('Y-m-d 20:00:00', strtotime('-1 days'));
        return LoDe::model()->findAll("play_date > :play_date", array(':play_date' => $start_time));
    }

    public static function layDanhSachLuotChoiCuaNguoiChoi($player_id)
    {
        $sql = "SELECT a.*, b.lode_type_name
                    FROM ld_lode a join ld_lode_type b on a.lode_type = b.lode_type_id
                    WHERE a.player_id = :player_id
                    ORDER BY a.play_date DESC";
        return Yii::app()->db->createCommand($sql)->queryAll(TRUE, array(':player_id' => $player_id));
    }

    public static function layDanhSachPhongThan()
    {
        $sql = "SELECT a.*, b.lode_type_name, c.user_name as player_name
                FROM ld_lode a join ld_lode_type b on a.lode_type = b.lode_type_id join ld_user c on a.player_id = c.user_id
                WHERE a.win_money > 0
                ORDER BY a.play_date DESC, a.win_money DESC
                LIMIT 0, 10";
        return Yii::app()->db->createCommand($sql)->queryAll(TRUE);
    }

}